/**
 * @file hwmisc.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include <stdlib.h>
#include <stdio.h>

#include "global.h"
#include "misc_log.h"
#include "bsp_adc.h"

#define HW_GET_ADC(x)        bsp_adc_getvalue(x)
#define HW_GET_ADC_ENABLE(x) bsp_adc_enable(x)

/* 硬件版本 */
struct pcba_ver_t {
    unsigned short ad_min;
    unsigned short ad_max;
    char           *str_hw;
};

//硬件版本相关信息
typedef struct {
    unsigned char done;                    //硬件版本读取标志
    char          verdesc[6];              //版本描述信息"V1.1"
    int32_t           verno;                   //版本号方便用于版本比较，放大100倍=>101,102,103
}hwver_info_t;

/* 硬件版本信息 */
static const struct pcba_ver_t pcba_ver_tab[] = {
    { 0x0FA3, 0x0FFF, "1.01" },
    { 0x0000, 0x005D, "1.02" },
    { 0x064E, 0x0726, "1.03" },
    { 0x0575, 0x064D, "1.04" },
    { 0x049C, 0x0574, "1.05" },
    { 0x03C3, 0x049B, "1.06" },
    { 0x02EA, 0x03C2, "1.07" },
    { 0x0210, 0x02E9, "1.08" },
    { 0x0137, 0x020F, "1.09" },
    { 0x005E, 0x0136, "1.10" },
    { 0x0727, 0x07FF, "1.11" },
    { 0x0800, 0x08D9, "1.12" },
    { 0x08DA, 0x09B2, "1.13" },
    { 0x09B3, 0x0A8B, "1.14" },
    { 0x0A8C, 0x0B64, "1.15" },
    { 0x0B65, 0x0C3D, "1.16" },
    { 0x0C3E, 0x0D16, "1.17" },
    { 0x0D17, 0x0DF0, "1.18" },
    { 0x0DF1, 0x0EC9, "1.19" },
    { 0x0ECA, 0x0FA2, "1.20" }
};

static hwver_info_t hwver;

/*
 * @brief       根据电压获取硬件版本索引
 * @param[in]   adcval - ADC转换值
 * @return      < 0 - 获取失败, >=0 获取成功
 */
static int32_t find_pcba_index(uint32_t adcval)
{
    int32_t i;
    for (i = 0; i < sizeof(pcba_ver_tab) / sizeof(pcba_ver_tab[0]); i++) {
        if ((adcval >= pcba_ver_tab[i].ad_min) &&
            (adcval <= pcba_ver_tab[i].ad_max)) {
            return i;
        }
    }
    return -1;
}


/*
 * @brief       读取硬件版本号(阻塞方式,硬件上需要兼容目的是开机就能直接获取版本号)
 * @param[in]   val - ADC转换值
 * @return
 */
void misc_read_hwver(void)
{
    int32_t  i;
    uint32_t val = 0;

    HW_GET_ADC_ENABLE(1);
    val = HW_GET_ADC(50);
    HW_GET_ADC_ENABLE(0);

    i = find_pcba_index(val);
    if (i < 0) {
        elog_warn("PUBLIC","HWVer read Fail\r\n");
        i = 2;
    }
    hwver.done = 1;
    memcpy(hwver.verdesc, pcba_ver_tab[i].str_hw, sizeof(hwver.verdesc));
    hwver.verno = (int32_t)((atof(pcba_ver_tab[i].str_hw) + 0.0001) * 100);

    log_i("hardware version =%s!\r\n", hwver.verdesc);
}


#include "FreeRTOS.h"
#include "task.h"
/*
 * @brief       获取硬件版本号
 * @param[in]   none
 * @return      硬件版本号(101表示1.01)
 */
int32_t get_hw_verno(void)
{
    return hwver.verno;
}


/*
 * @brief       获取硬件版本
 * @param[in]   none
 * @return      硬件版本("1.01")
 */
const char *get_hw_ver(void)
{
    return hwver.verdesc;
}



// #include "lettershell/shell.h"
// /**
//  * @brief  shell 测试接口
//  */
// int32_t shell_hw_version(void)
// {
//     printf("the hardware version of IoT is: %s\r\n", get_hw_ver());

//     return 0;
// }
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC) | SHELL_CMD_DISABLE_RETURN,
//                  show_hw_version, shell_hw_version, show the hw version of IoT);